package com.bayair.doctor.ui.inquiry;

import com.bayair.doctor.R;
import com.bayair.doctor.biz.im.ContactHelper;
import com.bayair.doctor.data.bean.ServerListResp;
import com.bayair.doctor.mvp.BasePresenter;
import com.bayair.doctor.mvp.annotation.InjectModel;
import com.bayair.doctor.net.RxFunction;
import com.bayair.doctor.net.RxSchedulers;
import com.bayair.doctor.ui.inquiry_record.SessionDataKit;
import com.tencent.qcloud.tim.uikit.modules.contact.ContactItemBean;

import java.util.List;


public class InquiryPresenter extends BasePresenter<InquiryContract.View> implements InquiryContract.Presenter {

    @InjectModel
    InquiryModel mModel;

    @Override
    public void getListInServer() {
        mModel.getListInServer()
                .map(new RxFunction<>())
                .compose(RxSchedulers.io_main())
                .subscribe(new ObserverOnRecycler<ServerListResp>() {
                    @Override
                    public void onNext(ServerListResp patientsInServerResp) {
                        List<ServerListResp.PatientsBean> patients = patientsInServerResp.getPatients();
                        SessionDataKit.instance().setListInServer(patients);
                        loadContact();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        getView().onError();
                    }
                });

    }

    //从IM中获取好友并根据服务器返回的响应过滤服务中的好友更新
    private void loadContact() {
        ContactDataKit.instance().loadContact(new ContactCallback() {
            @Override
            public void setList(List<ContactItemBean> list) {
                //刷新ui
                if (list.isEmpty()) {
                    getView().onEmpty();
                } else {
                    getView().updateContact(list);
                    getView().onComplete();
                }
            }

            @Override
            public void setError() {
                getView().onError();
            }
        });
    }

}
